//
//  MineCollectMainCollectionCell.m
//  ArtDog
//
//  Created by dllo on 16/9/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineCollectMainCollectionCell.h"
#import "BaseCollectionView.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "ZWYNetTool.h"
#import "HLMacro.h"
#import "UIView+Common.h"
#import "NSString+Common.h"
#import "ReadMainModel.h"
#import "HalfImageAndWordsCollectionCell.h"
#import "MineCollectGalleryList.h"
#import "MineCollectGalleryCollectionCell.h"
#import "ImageViewCollectionCell.h"
#import "CommunityPicture.h"
#import "ImageCollectViewController.h"
#import "MineCollectExhibitList.h"
#import "MineCollectExhibitCollectionCell.h"
#import "HomeExhibitWorkList.h"
#import "StoreGoodsCollectionCell.h"
#import "MineCollectGoods.h"
#import "MineCollectGoodsCollectionCell.h"

@interface MineCollectMainCollectionCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ImageCollectViewControllerDelegate>

@property (weak, nonatomic) IBOutlet BaseCollectionView *collectionView;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *loadArray;


@end

@implementation MineCollectMainCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if (_loadArray == nil) {
        NSMutableArray *topicList = [NSMutableArray array];
        _loadArray = [NSMutableArray arrayWithObjects:topicList, topicList, topicList, topicList, topicList, topicList, nil];
    }
    [self setupBasic];
}

- (void)setupBasic
{
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HalfImageAndWordsCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"halfImageAndWordsCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"MineCollectGalleryCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"mineCollectGalleryCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageViewCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageViewCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"MineCollectExhibitCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"mineCollectExhibitCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"StoreGoodsCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"storeGoodsCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"MineCollectGoodsCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"mineCollectGoodsCollectionCell"];
}

- (void)setType:(NSInteger)type
{
    
    _type = type;
    [_collectionView reloadData];
    if (_collectionView.mj_header == nil) {
        [self refreshCollectionView];
    }
    if (_loadArray[type].count == 0) {
        [self handleData];
    }
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray[_type] = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            if (_type == 0) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    ReadMainModel *model = [ReadMainModel modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 1) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectGalleryList *model = [MineCollectGalleryList modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 2) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    CommunityPicture *model = [CommunityPicture modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                    if (GETCOLLECTIMAGE == nil) {
                        SETCOLLECTIMAGE([NSDictionary dictionary]);
                    }
                    NSMutableDictionary *collectDic = [GETCOLLECTIMAGE mutableCopy];
                    [collectDic setValue:model.picPath forKey:model.picPath];
                    SETCOLLECTIMAGE(collectDic);
                }
            } else if (_type == 3) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectExhibitList *model = [MineCollectExhibitList modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 4) {
                if (![result[@"data"] isKindOfClass:[NSArray class]]) {
                    for (NSDictionary *dicTwo in result[@"data"][@"works"]) {
                        HomeExhibitWorkList *model = [HomeExhibitWorkList modelWithDic:dicTwo];
                        [_loadArray[_type] addObject:model];
                    }
                }
            } else {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectGoods *model = [MineCollectGoods modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            }
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            if (_type == 0) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    ReadMainModel *model = [ReadMainModel modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 1) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectGalleryList *model = [MineCollectGalleryList modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 2) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    CommunityPicture *model = [CommunityPicture modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                    NSMutableDictionary *collectDic = [GETCOLLECTIMAGE mutableCopy];
                    [collectDic setValue:model.picPath forKey:model.picPath];
                    SETCOLLECTIMAGE(collectDic);
                }
            } else if (_type == 3) {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectExhibitList *model = [MineCollectExhibitList modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            } else if (_type == 4) {
                if (![result[@"data"] isKindOfClass:[NSArray class]]) {
                    for (NSDictionary *dicTwo in result[@"data"][@"works"]) {
                        HomeExhibitWorkList *model = [HomeExhibitWorkList modelWithDic:dicTwo];
                        [_loadArray[_type] addObject:model];
                    }
                }
            } else {
                if ([result[@"data"] isKindOfClass:[NSArray class]]) {
                    MineCollectGoods *model = [MineCollectGoods modelWithDic:dic];
                    [_loadArray[_type] addObject:model];
                }
            }
        }
        [_collectionView.mj_footer endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    NSString *type = @"";
    
    if (_type == 0) {
        type = @"4";
    } else if (_type == 1) {
        type = @"1";
    } else if (_type == 2) {
        type = @"2";
    } else if (_type == 3) {
        urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v3/listLikesTopicAndExhibit"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"otherUserId": [NSString getUserDefaultId], @"likeType": @"exhibit"}];
    } else if (_type == 4) {
        urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"new_likes"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"otherUserId": [NSString getUserDefaultId]}];
    } else {
        type = @"8";
    }
    if (_type < 3 || _type == 5) {
        urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v3/user/myCollections"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"otherUserId": [NSString getUserDefaultId], @"type": type}];
    }
    return urlString;
}

#pragma mark - collectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _loadArray[_type].count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 0) {
        HalfImageAndWordsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"halfImageAndWordsCollectionCell" forIndexPath:indexPath];
        cell.readModel = _loadArray[_type][indexPath.item];
        return cell;
    } else if (_type == 1) {
        MineCollectGalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mineCollectGalleryCollectionCell" forIndexPath:indexPath];
        cell.galleryModel = _loadArray[_type][indexPath.item];
        return cell;
    } else if (_type == 2) {
        ImageViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCollectionCell" forIndexPath:indexPath];
        CommunityPicture *model = _loadArray[_type][indexPath.item];
        cell.urlImage = model.picPath;
        return cell;
    } else if (_type == 3) {
        MineCollectExhibitCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mineCollectExhibitCollectionCell" forIndexPath:indexPath];
        cell.exhibitModel = _loadArray[_type][indexPath.item];
        return cell;
    } else if (_type == 4) {
        StoreGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"storeGoodsCollectionCell" forIndexPath:indexPath];
        cell.exhibitWorkModel = _loadArray[_type][indexPath.item];
        return cell;
    } else {
        MineCollectGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mineCollectGoodsCollectionCell" forIndexPath:indexPath];
        cell.goodsModel = _loadArray[_type][indexPath.item];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 2) {
        ImageCollectViewController *imageVC = [[ImageCollectViewController alloc]init];
        imageVC.delegate = self;
        imageVC.isCollectPage = YES;
        imageVC.pictures = _loadArray[_type];
        imageVC.indexPath = indexPath;
        UIViewController *vc = [self getVCRecently];
        [vc.navigationController pushViewController:imageVC animated:YES];
        
    }
}

- (void)cancelCollect:(CommunityPicture *)picture
{
    if (_type == 2) {
        [_loadArray[_type] removeObject:picture];
        [_collectionView reloadData];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 0) {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 3.6);
    } else if (_type == 1) {
        return CGSizeMake(SCREEN_WIDTH, (SCREEN_WIDTH - 30) / 1.618 + 50);
    } else if (_type == 2) {
        return CGSizeMake((SCREEN_WIDTH - 50) / 3, (SCREEN_WIDTH - 50) / 3);
    } else if (_type == 3 || _type == 5) {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 4 + 30);
    } else if (_type == 4) {
        return CGSizeMake((SCREEN_WIDTH - 50) / 2, (SCREEN_WIDTH - 50) / 2 * 1.2 + 100);
    }

    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 3.6);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (_type == 2 || _type == 4) {
        return UIEdgeInsetsMake(0, 15, 0, 15);
    } else {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (_type == 2) {
        return 10;
    } else {
        return 5;
    }
}


#pragma mark - 刷新
- (void)refreshCollectionView
{
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_collectionView.mj_header mjRefreshStateHeaderStyle];
    
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray[_type].count % 10 == 0) {
            _pageIndex = _loadArray[_type].count / 10 + 1;
        } else {
            _pageIndex = _loadArray[_type].count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_collectionView.mj_footer mjRefreshAutoStateFooterStyle];
}
@end
